﻿using AliIotAmqpWorker.Common;
using IServices;
using System;
using System.Collections.Generic;
using System.Text;
using YL.Core.Entity;

namespace AliIotAmqpWorker
{
    public class DeviceFailureService
    {
        private readonly IMeter_DeviceFailureHistoryServices deviceFailureHistoryServices;
        private readonly IMeter_DeviceFailureDetailServices deviceFailureDetailServices;
        private readonly IMeter_DeviceInfoServices deviceInfoServices;
        private readonly IMeter_ClientInfoServices clientInfoServices;
        private readonly IMeter_DeviceFailureDayServices deviceFailureDayServices;
        private readonly IMeter_DeviceFailureMonthServices deviceFailureMonthServices;

        public DeviceFailureService(IMeter_DeviceFailureHistoryServices DeviceFailureHistoryServices,
                                    IMeter_DeviceFailureDetailServices DeviceFailureDetailServices,
                                    IMeter_DeviceInfoServices DeviceInfoServices,
                                    IMeter_ClientInfoServices ClientInfoServices,
                                    IMeter_DeviceFailureDayServices meter_DeviceFailureDayServices,
                                    IMeter_DeviceFailureMonthServices meter_DeviceFailureMonthServices)
        {
            deviceFailureHistoryServices = DeviceFailureHistoryServices;
            deviceFailureDetailServices = DeviceFailureDetailServices;
            deviceInfoServices = DeviceInfoServices;
            clientInfoServices = ClientInfoServices;
            deviceFailureDayServices = meter_DeviceFailureDayServices;
            deviceFailureMonthServices = meter_DeviceFailureMonthServices;
        }

        /// <summary>
        /// 新增错误
        /// </summary>
        /// <param name="deviceData"></param>
        /// <param name="device"></param>
        /// <param name="failureDetail"></param>
        public void AddFailure(AliIotDeviceData<ECU1151Info> deviceData, DeviceInfo device, string failureDetail)
        {
            ClientInfo clientInfo = clientInfoServices.QueryableToEntity(x => x.ClientId == device.ClientId);
            DeviceFailureHistory deviceFailure = new DeviceFailureHistory();
            deviceFailure.FailureDetail = failureDetail;
            deviceFailure.FailureEquip = clientInfo == null ? clientInfo.ClientName : string.Empty;
            deviceFailure.StartTime = DateTime.Now;
            deviceFailure.EndTime = GlobalSetting.DefaultEndTime;
            deviceFailure.ConfirmTime= GlobalSetting.DefaultEndTime;
            deviceFailure.DeviceId = device.DeviceId;
            long id = deviceFailureHistoryServices.InsertBigIdentity(deviceFailure);
            if (id > 0)
            {
                AddFailureDetail(device.DeviceId, (int)id, deviceData);
                AddFailureDay(device, clientInfo);
            }
        }
        /// <summary>
        /// 更新故障结束时间
        /// </summary>
        /// <param name="deviceData"></param>
        /// <param name="device"></param>
        /// <param name="FailureDetail"></param>
        /// <param name="FailureType"></param>
        public bool UpdateFailure(AliIotDeviceData<ECU1151Info> deviceData, DeviceInfo device, string failureDetail)
        {
            if (device != null)
            {
                DeviceFailureHistory deviceFailure = deviceFailureHistoryServices.QueryableToEntity(x => x.DeviceId == device.DeviceId && x.EndTime == GlobalSetting.DefaultEndTime && x.FailureDetail == failureDetail);
                if (deviceFailure != null)
                {
                    deviceFailure.EndTime = DateTime.Now;
                    if( deviceFailureHistoryServices.UpdateEntity(deviceFailure))
                    {
                        UpdateFailureDay(device, deviceFailure);
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 增加故障天数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void AddFailureDay(DeviceInfo device, ClientInfo clientInfo)
        {
            DeviceFailureDay deviceFailureDay = new DeviceFailureDay();

            deviceFailureDay = deviceFailureDayServices.QueryableToEntity(x => x.Date == DateTime.Now.Date && x.DeviceId == device.DeviceId);
            if (deviceFailureDay == null)
            {
                deviceFailureDay = new DeviceFailureDay();
                deviceFailureDay.FailureCount = 1;
                deviceFailureDay.Date = DateTime.Now.Date;
                deviceFailureDay.DeviceId = device.DeviceId;
                deviceFailureDay.DeviceName = device.DeviceName;
                deviceFailureDay.ClientName = clientInfo.ClientName;
                deviceFailureDayServices.InsertBigIdentity(deviceFailureDay);
            }
            else
            {
                deviceFailureDay.FailureCount = deviceFailureDay.FailureCount + 1;
                deviceFailureDayServices.UpdateEntity(deviceFailureDay);
            }
            AddFailureMonth(device);//增加月数据
        }
        /// <summary>
        /// 更新故障天数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void UpdateFailureDay(DeviceInfo device, DeviceFailureHistory deviceFailure)
        {
            DeviceFailureDay deviceFailureDay = new DeviceFailureDay();
            TimeSpan timeSpan = deviceFailure.ConfirmTime - deviceFailure.StartTime;
            deviceFailureDay = deviceFailureDayServices.QueryableToEntity(x => x.Date == DateTime.Now.Date && x.DeviceId == device.DeviceId);
            if (deviceFailureDay == null)
            {
                deviceFailureDay = new DeviceFailureDay();
                deviceFailureDay.FailureCount = 1;
                deviceFailureDay.DurationMin = timeSpan.TotalMinutes;
                deviceFailureDay.ConfirmTime = timeSpan.TotalMinutes;
                deviceFailureDay.Date = DateTime.Now.Date;
                deviceFailureDay.DeviceId = device.DeviceId;
                deviceFailureDay.DeviceName = device.DeviceName;
                deviceFailureDayServices.InsertBigIdentity(deviceFailureDay);
            }
            else
            {
                deviceFailureDay.DurationMin += timeSpan.TotalMinutes;
                deviceFailureDay.ConfirmTime += timeSpan.TotalMinutes;
                deviceFailureDayServices.UpdateEntity(deviceFailureDay);
            }
            UpdateFailureMonth(device, deviceFailure);//更新月数据
        }
        /// <summary>
        /// 增加故障月数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void AddFailureMonth(DeviceInfo device)
        {
            DateTime dtNow = DateTime.Now;
            int year = dtNow.Year;
            int month = dtNow.Month;
            DeviceFailureMonth deviceFailureMonth = new DeviceFailureMonth();

            deviceFailureMonth = deviceFailureMonthServices.QueryableToEntity(x => x.Year == year &&
                                                                                   x.Month == month &&
                                                                                   x.Date == dtNow.Date &&
                                                                                   x.DeviceId == device.DeviceId);
            if (deviceFailureMonth == null)
            {
                deviceFailureMonth = new DeviceFailureMonth();
                deviceFailureMonth.FailureCount = 1;
                deviceFailureMonth.Year = dtNow.Year;
                deviceFailureMonth.Month = dtNow.Month;
                deviceFailureMonth.Date = DateTime.Now.Date;
                deviceFailureMonth.DeviceId = device.DeviceId;
                deviceFailureMonthServices.InsertBigIdentity(deviceFailureMonth);
            }
            else
            {
                deviceFailureMonth.FailureCount += 1;
                deviceFailureMonthServices.UpdateEntity(deviceFailureMonth);
            }
        }
        /// <summary>
        /// 更新故障月数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void UpdateFailureMonth(DeviceInfo device, DeviceFailureHistory deviceFailure)
        {
            DateTime dtNow = DateTime.Now;
            int year = dtNow.Year;
            int month = dtNow.Month;
            DeviceFailureMonth deviceFailureMonth = new DeviceFailureMonth();
            TimeSpan timeSpan = deviceFailure.ConfirmTime - deviceFailure.StartTime;
            deviceFailureMonth = deviceFailureMonthServices.QueryableToEntity(x => x.Year == year &&
                                                                                   x.Month == month &&
                                                                                   x.Date == dtNow.Date &&
                                                                                   x.DeviceId == device.DeviceId);
            if (deviceFailureMonth == null)
            {
                deviceFailureMonth = new DeviceFailureMonth();
                deviceFailureMonth.FailureCount = 1;
                deviceFailureMonth.DurationMin = timeSpan.TotalMinutes;
                deviceFailureMonth.ConfirmTime = timeSpan.TotalMinutes;
                deviceFailureMonth.Date = DateTime.Now.Date;
                deviceFailureMonth.DeviceId = device.DeviceId;
                deviceFailureMonthServices.InsertBigIdentity(deviceFailureMonth);
            }
            else
            {
                deviceFailureMonth.DurationMin += timeSpan.TotalMinutes;
                deviceFailureMonth.ConfirmTime += timeSpan.TotalMinutes;
                deviceFailureMonthServices.UpdateEntity(deviceFailureMonth);
            }
        }

        /// <summary>
        /// 新增错误明细
        /// </summary>
        /// <param name="DeviceId"></param>
        /// <param name="HistoryId"></param>
        /// <param name="deviceData"></param>
        public void AddFailureDetail(int DeviceId, int HistoryId, AliIotDeviceData<ECU1151Info> deviceData)
        {
            //AddFailureDetail(DeviceId, HistoryId, "A相电压", deviceData.items.Ua.value);
            //AddFailureDetail(DeviceId, HistoryId, "B相电压", deviceData.items.Ub.value);
            //AddFailureDetail(DeviceId, HistoryId, "C相电压", deviceData.items.Uc.value);
            //AddFailureDetail(DeviceId, HistoryId, "A相电流", deviceData.items.Ia.value);
            //AddFailureDetail(DeviceId, HistoryId, "B相电流", deviceData.items.Ib.value);
            //AddFailureDetail(DeviceId, HistoryId, "C相电流", deviceData.items.Ic.value);
            //AddFailureDetail(DeviceId, HistoryId, "温度通道1", deviceData.items.T1.value);
            //AddFailureDetail(DeviceId, HistoryId, "温度通道2", deviceData.items.T2.value);
            //AddFailureDetail(DeviceId, HistoryId, "温度通道3", deviceData.items.T3.value);
            //AddFailureDetail(DeviceId, HistoryId, "温度通道4", deviceData.items.T4.value);
        }
        /// <summary>
        /// 错误明细
        /// </summary>
        /// <param name="DeviceId"></param>
        /// <param name="HistoryId"></param>
        /// <param name="ParameterName"></param>
        /// <param name="ParameterValue"></param>
        private void AddFailureDetail(int DeviceId, int HistoryId, string ParameterName, object ParameterValue)
        {
            DeviceFailureDetail deviceFailureDetail = new DeviceFailureDetail();
            deviceFailureDetail.HistoryId = HistoryId;
            deviceFailureDetail.ParameterName = ParameterName;
            deviceFailureDetail.ParameterValue = ParameterValue.ToString();
            deviceFailureDetail.CreateDate = DateTime.Now;
            deviceFailureDetailServices.Insert(deviceFailureDetail);
        }
    }
}
